const TOOLBAR =[
	{content:"添加", icon:"el-icon-document-add",type:"primary"},
]
const COLUMNS = [
	{prop:"name", label:"名称",width:"80"},
	{prop:"spec", label:"规格",width:"50"},
	{prop:"type", label:"类别",width:"100"},
	{prop:"unit", label:"单位",width:"100"},
	{prop:"price", label:"单价",width:"80"},
	{prop:"countup", label:"赠送上限",width:"80"},
	{prop:"amountup", label:"服务限价",width:"80"},
	{prop:"number", label:"服务周期",width:"80"},
	{prop:"cost", label:"成本金额",width:"80"},
	{prop:"items", label:"服务项目",width:"100"},
	{prop:"isactive", label:"是否可用",width:"80"},
	{prop:"memo", label:"备注",width:"80"},

	
]

const servicerules={
	name:[{
		required: true,
		message: '请选择服务名称',
		trigger: 'blur'
	}]
};

const serviceDetailsRules = {
serviceId:[{
	required: true,
	message: '请选择服务名称',
	trigger: 'blur'
}],
price:[{
	required: true,
	message: '请输入单价',
	trigger: 'blur'
}],
cost:[{
	required: true,
	message: '请选择成本金额',
	trigger: 'blur'
}],
isactive:[{
	required: true,
	message: '请选择是否可用',
	trigger: 'blur'
}],
};
			
let app = new Vue({
    el: "#app",
    data() {
        return {
        	leftbar_show:true,
			headers: {
				Token: localStorage.getItem('TOKEN'),
				UserID: localStorage.getItem('USERID')
			},
			// 添加、编辑标识
			action:"",
			tableHeight:window.innerHeight - 180,
			toolbar:TOOLBAR,
			tablecolums:COLUMNS,
			services:[],
			servicename:"",
			dialogFormVisible:false,
			treeshow:false,
			serviceForm:{},
			serviceFormDetail:{},
			combinationForm:{
				defaultProps: {
				          children: 'children',
				          label: 'label'
				},
				
			
			 data: [],
					
			      },
			queryForm:{
				serviceId:0,
				pageNo:1, // 当前页码
				pagesize:100 ,// 每页记录数
			},
			pagetotal:0,
			// 所有部门
			depts:[],
			// 所有用户
			users:[],
			actives:['是','否'],

			multipleSelection:[],
			pagedata:[],  // 当前页结果集
			loaded:false,
			addServiceFormVisible :false,
			queryFormVisible :false,
        }
},
	methods: {
		// 显示侧边栏
		showLeftBar() {
			if (this.leftbar_show) {
				this.leftbar_show = false
			} else {
				this.leftbar_show = true
			}
		},
		handleNodeClick(data) {
			console.log(data);
			this.servicename = data.label
			this.queryForm.serviceId = data.id;
			this.queryForm.pageNo = 1;
			this.query();
		},

	
 
       
		// 表格多选 
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
		//添加服务确认
		async addserviceconfirm(){
			this.$refs.serviceForm.validate(async valid => {
			    if (valid){
					// 显示加载框
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res =null
						if(this.action =="addservice"){
							 res = await axios.post('api/service/addname',this.serviceForm);
						}else if(this.action =="editservice"){
							 res = await axios.put('api/service/editname',this.serviceForm);
						}
						
						if (res.code === 200) {
							this.addServiceFormVisible = false;
							this.getServiceTree();
							this.getService();
						}else {
							this.$message.error(res.message);
						}
					}catch (e) {
						this.$message.error('请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
			    }
			})	
		},
		// 添加服务详情确认
		async cofirm(){
            this.$refs.serviceFormDetail.validate(async valid => {
                if (valid){
					// 显示加载框
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res =null
						if(this.action =="add"){
							res = await axios.post('api/service/add',this.serviceFormDetail);
						}else if(this.action =="edit"){
							res = await axios.put('api/service/edit',this.serviceFormDetail);
						}
						
						if (res.code === 200) {
							this.dialogFormVisible = false;
							this.$message.success(res.message);
							this.query()
						}else {
							this.$message.error(res.message);
						}
					}catch (e) {
						this.$message.error('请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
                }
            })			
		},
		
		// 取得服务树状图

		async getServiceTree() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			this.treeshow = false;
			try {
				let res = await axios.get('api/servicetree/get');
				if (res.code === 200) {
					this.combinationForm.data =res.data

				
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('服务项目请求出错！', e.message);
			}
			this.treeshow = true;
			// 关闭加载框
			loading.close();
		},
	
		start(){
			this.queryForm.pageNo = 1
			this.query()
		},
		// 查询
		async query() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.post('api/servicedetail/get/methodpost',this.queryForm);
				if (res.code === 200) {
					this.pagedata = res.data;
					this.pagetotal = res.total;
					this.$message.success(res.message);
				}else {
					this.$message.error(res.message);
				}
			}catch (e) {
				this.$message.error('请求出错！', e.message);
			}
			
			// 关闭加载框
			loading.close();
		},
		toolAction(acName) {
			if (acName == "添加") {
				this.add()
			} 
		},
		actionQuery() {
			this.query();
			this.queryFormVisible=false;
		},
	
		selectionChange(selection){
			this.selection = selection
		},
		// 设定表格当前页
		handleCurrentPageChange (selectedPageNo) {
		  this.queryForm.pageNo = selectedPageNo
		  // 加载数据页
		  this.query()
		},
		// 取得所有服务项目
		async getService() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.get('api/service/get');
				if (res.code === 200) {
					this.services = res.data;
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('服务项目请求出错！', e.message);
			}
			

			// 关闭加载框
			loading.close();
		},
		editservice(){
			this.action = "editservice"
			this.addServiceFormVisible= true;
			this.serviceForm= {
				serviceId:this.queryForm.serviceId=null?0:this.queryForm.serviceId,
				name : this.servicename=null?null:this.servicename,
				time:"",
				unit:"",
				memo:"",
				dept_id:null
			}
		},
		addservice(){
			this.action = "addservice"
			this.addServiceFormVisible= true;
			
			this.serviceForm= {
				name : "",
				time:"",
				unit:"",
				memo:"",
				dept_id:null
			}
			
		},
		// 添加
		add() {
			this.action="add";
			this.dialogFormVisible = true;
			serviceId =this.queryForm.serviceId=0?null:this.queryForm.serviceId
            this.serviceFormDetail= {
						serviceId : serviceId,
						price : 0,
						isactive : "",
						unit : "",
						spec : "",
						type :"",
						cost : 0,
						countup:0,
						amountup:0,
						number:0,
						items : "",
						memo : ""
            }
		},
		// 添加
		edit(index,row) {
			this.action="edit";
			this.dialogFormVisible = true;
            this.serviceFormDetail=  deepCopy(row);
			this.serviceFormDetail.serviceId =this.queryForm.serviceId=0?null:this.queryForm.serviceId
		},		
	},
	async mounted() {
		// 自动调整表格高度
		window.onresize = () => {
		  return (() => {
			this.tableHeight = window.innerHeight - 180
		  })()
		}
		// 取得服务列表
		this.getServiceTree();
		// 取得服务
		this.getService();
		// 初始化用户列表
		this.query();
	}

});	